<?php


abstract class BaseRingtoneCategory extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $name;


	
	protected $avatar_path;


	
	protected $parent_id;


	
	protected $bonus = 300;


	
	protected $description;


	
	protected $wap_order = 0;


	
	protected $enabled;


	
	protected $web_order;

	
	protected $aRingtoneCategoryRelatedByParentId;

	
	protected $collRingtoneCategorysRelatedByParentId;

	
	protected $lastRingtoneCategoryRelatedByParentIdCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getName()
	{

		return $this->name;
	}

	
	public function getAvatarPath()
	{

		return $this->avatar_path;
	}

	
	public function getParentId()
	{

		return $this->parent_id;
	}

	
	public function getBonus()
	{

		return $this->bonus;
	}

	
	public function getDescription()
	{

		return $this->description;
	}

	
	public function getWapOrder()
	{

		return $this->wap_order;
	}

	
	public function getEnabled()
	{

		return $this->enabled;
	}

	
	public function getWebOrder()
	{

		return $this->web_order;
	}

	
	public function setId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::ID;
		}

	} 
	
	public function setName($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->name !== $v) {
			$this->name = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::NAME;
		}

	} 
	
	public function setAvatarPath($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->avatar_path !== $v) {
			$this->avatar_path = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::AVATAR_PATH;
		}

	} 
	
	public function setParentId($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->parent_id !== $v) {
			$this->parent_id = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::PARENT_ID;
		}

		if ($this->aRingtoneCategoryRelatedByParentId !== null && $this->aRingtoneCategoryRelatedByParentId->getId() !== $v) {
			$this->aRingtoneCategoryRelatedByParentId = null;
		}

	} 
	
	public function setBonus($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->bonus !== $v || $v === 300) {
			$this->bonus = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::BONUS;
		}

	} 
	
	public function setDescription($v)
	{

		
		
		if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->description !== $v) {
			$this->description = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::DESCRIPTION;
		}

	} 
	
	public function setWapOrder($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->wap_order !== $v || $v === 0) {
			$this->wap_order = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::WAP_ORDER;
		}

	} 
	
	public function setEnabled($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->enabled !== $v) {
			$this->enabled = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::ENABLED;
		}

	} 
	
	public function setWebOrder($v)
	{

		
		
		if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->web_order !== $v) {
			$this->web_order = $v;
			$this->modifiedColumns[] = RingtoneCategoryPeer::WEB_ORDER;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->name = $rs->getString($startcol + 1);

			$this->avatar_path = $rs->getString($startcol + 2);

			$this->parent_id = $rs->getInt($startcol + 3);

			$this->bonus = $rs->getInt($startcol + 4);

			$this->description = $rs->getString($startcol + 5);

			$this->wap_order = $rs->getInt($startcol + 6);

			$this->enabled = $rs->getInt($startcol + 7);

			$this->web_order = $rs->getInt($startcol + 8);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 9; 
		} catch (Exception $e) {
			throw new PropelException("Error populating RingtoneCategory object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(RingtoneCategoryPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			RingtoneCategoryPeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(RingtoneCategoryPeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


												
			if ($this->aRingtoneCategoryRelatedByParentId !== null) {
				if ($this->aRingtoneCategoryRelatedByParentId->isModified()) {
					$affectedRows += $this->aRingtoneCategoryRelatedByParentId->save($con);
				}
				$this->setRingtoneCategoryRelatedByParentId($this->aRingtoneCategoryRelatedByParentId);
			}


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = RingtoneCategoryPeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += RingtoneCategoryPeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collRingtoneCategorysRelatedByParentId !== null) {
				foreach($this->collRingtoneCategorysRelatedByParentId as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


												
			if ($this->aRingtoneCategoryRelatedByParentId !== null) {
				if (!$this->aRingtoneCategoryRelatedByParentId->validate($columns)) {
					$failureMap = array_merge($failureMap, $this->aRingtoneCategoryRelatedByParentId->getValidationFailures());
				}
			}


			if (($retval = RingtoneCategoryPeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}



			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = RingtoneCategoryPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getName();
				break;
			case 2:
				return $this->getAvatarPath();
				break;
			case 3:
				return $this->getParentId();
				break;
			case 4:
				return $this->getBonus();
				break;
			case 5:
				return $this->getDescription();
				break;
			case 6:
				return $this->getWapOrder();
				break;
			case 7:
				return $this->getEnabled();
				break;
			case 8:
				return $this->getWebOrder();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = RingtoneCategoryPeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getName(),
			$keys[2] => $this->getAvatarPath(),
			$keys[3] => $this->getParentId(),
			$keys[4] => $this->getBonus(),
			$keys[5] => $this->getDescription(),
			$keys[6] => $this->getWapOrder(),
			$keys[7] => $this->getEnabled(),
			$keys[8] => $this->getWebOrder(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = RingtoneCategoryPeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setName($value);
				break;
			case 2:
				$this->setAvatarPath($value);
				break;
			case 3:
				$this->setParentId($value);
				break;
			case 4:
				$this->setBonus($value);
				break;
			case 5:
				$this->setDescription($value);
				break;
			case 6:
				$this->setWapOrder($value);
				break;
			case 7:
				$this->setEnabled($value);
				break;
			case 8:
				$this->setWebOrder($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = RingtoneCategoryPeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setName($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setAvatarPath($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setParentId($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setBonus($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setDescription($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setWapOrder($arr[$keys[6]]);
		if (array_key_exists($keys[7], $arr)) $this->setEnabled($arr[$keys[7]]);
		if (array_key_exists($keys[8], $arr)) $this->setWebOrder($arr[$keys[8]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(RingtoneCategoryPeer::DATABASE_NAME);

		if ($this->isColumnModified(RingtoneCategoryPeer::ID)) $criteria->add(RingtoneCategoryPeer::ID, $this->id);
		if ($this->isColumnModified(RingtoneCategoryPeer::NAME)) $criteria->add(RingtoneCategoryPeer::NAME, $this->name);
		if ($this->isColumnModified(RingtoneCategoryPeer::AVATAR_PATH)) $criteria->add(RingtoneCategoryPeer::AVATAR_PATH, $this->avatar_path);
		if ($this->isColumnModified(RingtoneCategoryPeer::PARENT_ID)) $criteria->add(RingtoneCategoryPeer::PARENT_ID, $this->parent_id);
		if ($this->isColumnModified(RingtoneCategoryPeer::BONUS)) $criteria->add(RingtoneCategoryPeer::BONUS, $this->bonus);
		if ($this->isColumnModified(RingtoneCategoryPeer::DESCRIPTION)) $criteria->add(RingtoneCategoryPeer::DESCRIPTION, $this->description);
		if ($this->isColumnModified(RingtoneCategoryPeer::WAP_ORDER)) $criteria->add(RingtoneCategoryPeer::WAP_ORDER, $this->wap_order);
		if ($this->isColumnModified(RingtoneCategoryPeer::ENABLED)) $criteria->add(RingtoneCategoryPeer::ENABLED, $this->enabled);
		if ($this->isColumnModified(RingtoneCategoryPeer::WEB_ORDER)) $criteria->add(RingtoneCategoryPeer::WEB_ORDER, $this->web_order);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(RingtoneCategoryPeer::DATABASE_NAME);

		$criteria->add(RingtoneCategoryPeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setName($this->name);

		$copyObj->setAvatarPath($this->avatar_path);

		$copyObj->setParentId($this->parent_id);

		$copyObj->setBonus($this->bonus);

		$copyObj->setDescription($this->description);

		$copyObj->setWapOrder($this->wap_order);

		$copyObj->setEnabled($this->enabled);

		$copyObj->setWebOrder($this->web_order);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getRingtoneCategorysRelatedByParentId() as $relObj) {
				if($this->getPrimaryKey() === $relObj->getPrimaryKey()) {
						continue;
				}

				$copyObj->addRingtoneCategoryRelatedByParentId($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new RingtoneCategoryPeer();
		}
		return self::$peer;
	}

	
	public function setRingtoneCategoryRelatedByParentId($v)
	{


		if ($v === null) {
			$this->setParentId(NULL);
		} else {
			$this->setParentId($v->getId());
		}


		$this->aRingtoneCategoryRelatedByParentId = $v;
	}


	
	public function getRingtoneCategoryRelatedByParentId($con = null)
	{
		if ($this->aRingtoneCategoryRelatedByParentId === null && ($this->parent_id !== null)) {
						include_once 'lib/model/om/BaseRingtoneCategoryPeer.php';

			$this->aRingtoneCategoryRelatedByParentId = RingtoneCategoryPeer::retrieveByPK($this->parent_id, $con);

			
		}
		return $this->aRingtoneCategoryRelatedByParentId;
	}

	
	public function initRingtoneCategorysRelatedByParentId()
	{
		if ($this->collRingtoneCategorysRelatedByParentId === null) {
			$this->collRingtoneCategorysRelatedByParentId = array();
		}
	}

	
	public function getRingtoneCategorysRelatedByParentId($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseRingtoneCategoryPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collRingtoneCategorysRelatedByParentId === null) {
			if ($this->isNew()) {
			   $this->collRingtoneCategorysRelatedByParentId = array();
			} else {

				$criteria->add(RingtoneCategoryPeer::PARENT_ID, $this->getId());

				RingtoneCategoryPeer::addSelectColumns($criteria);
				$this->collRingtoneCategorysRelatedByParentId = RingtoneCategoryPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(RingtoneCategoryPeer::PARENT_ID, $this->getId());

				RingtoneCategoryPeer::addSelectColumns($criteria);
				if (!isset($this->lastRingtoneCategoryRelatedByParentIdCriteria) || !$this->lastRingtoneCategoryRelatedByParentIdCriteria->equals($criteria)) {
					$this->collRingtoneCategorysRelatedByParentId = RingtoneCategoryPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastRingtoneCategoryRelatedByParentIdCriteria = $criteria;
		return $this->collRingtoneCategorysRelatedByParentId;
	}

	
	public function countRingtoneCategorysRelatedByParentId($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BaseRingtoneCategoryPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(RingtoneCategoryPeer::PARENT_ID, $this->getId());

		return RingtoneCategoryPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addRingtoneCategoryRelatedByParentId(RingtoneCategory $l)
	{
		$this->collRingtoneCategorysRelatedByParentId[] = $l;
		$l->setRingtoneCategoryRelatedByParentId($this);
	}

} 